var tel;
var qq;
var fax;
var email;
var webicp;
var webicp2;
var addr;
var postCode;
var comname;
var webname;
var webtitle;
var weblogo = "/default/images/default_logo.png";
var wechatlogo;
var certOfficialSeal;
var diyDomainCertUrl;
var introduce;
var supportCodeLogin;
var codeLoginWay;
var platformNavigation;

function getParam(){
    var service = {};
    service.type = 1;
    var fn="getparam";
    service = Commonjs.jsonToString(service);
    var params = Commonjs.getParams(fn,service);//获取参数
    var data=Commonjs.ajax(sysurl,params,false);
    if(data==null)
        return false;
    if(data.result=="success"){
        //存到localstorage
        window.localStorage.setItem(configParam.common.configStorageKey, JSON.stringify(data.data));
        BaseForeach(data.data, function (i, item) {
            switch(item.paramName){
                case "comtel":
                    tel=item.paramValue;
                    break;
                case "comqq":
                    qq=item.paramValue;
                    break;
                case "comfax":
                    fax=item.paramValue;
                    break;
                case "comemail":
                    email=item.paramValue;
                    break;
                case "webicp":
                    webicp=item.paramValue;
                    break;
                case "webicp2":
                    webicp2=item.paramValue;
                    break;
                case "comaddress":
                    addr=item.paramValue;
                    break;
                case "compostcode":
                    postCode=item.paramValue;
                    break;
                case "comname":
                    comname=item.paramValue;
                    break;
                case "webname":
                    webname=item.paramValue;
                    break;
                case "webtitle":
                    webtitle=item.paramValue;
                    break;
                case "weblogo":
                    if(item.paramValue == null || item.paramValue == '' || item.paramValue == undefined){
                        weblogo="/default/images/default_logo.png";
                    }else{
                        weblogo=item.paramValue;
                    }
                    break;
                case "wechatlogo":
                    wechatlogo=item.paramValue;
                    break;
                case "certOfficialSealImg":
                    certOfficialSeal=item.paramValue;
                    break;
                case "diyDomainCertUrl":
                    diyDomainCertUrl=item.paramValue;
                    break;
                case "introduce":
                    introduce=item.paramValue;
                    break;
                case "codeLoginWay":
                    codeLoginWay = item.paramValue;
                    break;
                case "supportCodeLogin":
                    supportCodeLogin = item.paramValue;
                    break;
                case "platformNavigation":
                    platformNavigation = item.paramValue;
                    break;
            }
        });
    }
}

$(function(){
    getParam();
    var heard="";
    heard+="<div class='top'>";
    heard+="  <div class='container of'>";
    heard+="    <div class='topl of fl'><a href='"+realPath+"/default/index.html'>欢迎访问"+webtitle+"</a></div>";
    heard+="    <div class='topr of fr'>";
    heard+="      <div class='topr-l fl' >";
    heard+="      	<a href='"+realPath+"/default/usercenter/shopping/shoppinglist.html' class='cart'>&nbsp;&nbsp;购物车&nbsp;<span id='cartCount'>0</span></a>";
    heard+="      	<a href='"+realPath+"/default/controlPanel.html' >控制面板</a>";
    heard+="      	<a href='javascript:void(0)' onClick='unicplogin();' >备案</a>";
    heard+="      </div>";
    heard+="      <div class='topr-r fr' id='yeslogged'>";
    heard+="      		&nbsp;<a href='"+realPath+"/default/usercenter/index.html' id='hluserName'></a>";
    heard+="      		&nbsp;<a class='ui' href='"+realPath+"/default/usercenter/index.html' >管理中心</a>";
    if ($.cookie(configParam.common.cfgKey.bgCookieName) == 'y') {
        heard+="      		&nbsp;<a class='ui' href='javascript:void(0);' onClick='changeManage()' >切回管理员</a>";
    }
    heard+="			&nbsp;<a href='javascript:void(0)' onClick='Cancellation();' >退出</a>";
    heard+="      </div>";
    heard+="      <div class='topr-r fr' id='nologged'>";
    heard+="			<a href='"+realPath+"/default/login.html'>登录</a>";
    heard+="      		<a href='"+realPath+"/default/register/register.html'>免费注册</a>";
    heard+="      </div>";
    heard+="    </div>";
    heard+="  </div>";
    heard+="</div>";
    heard+="<div align='left'></div>";
    heard+="   <div id='headermenu' class='container head of'>";
    heard+="       <div class='logo fl'><img src='" + weblogo + "' width='160' height='47'/></div>";
    heard+="       <div id='hul' class='nav'>";
    heard+="       </div>";
    heard+="   </div>";
    if(document.getElementById('header')) document.getElementById('header').innerHTML=heard;

    $('#hul').tpl('/default/tpl/nav.new', {}, function () {
        var navItem = platformNavigation[0].children;
        var navHtml = "";
        var tplUri = Commonjs.getCfgVal(configParam.common.cfgKey.template);

        var getLink = function (node) {
            var link = node.link;
            if (link.toLowerCase().indexOf("javascript") > -1) {
                return link;
            }
            if (link.toLowerCase().substring(0, 4) == "http") {
                return link;
            }
            if ("#" == link || "" == link) {
                return link;
            }
            if ("/" == link) {
                return tplUri;
            }
            return tplUri + link;
        };

        for (var i=0; i<navItem.length; i++) {
            var first = navItem[i];
            var url = getLink(first);
            navHtml += '<li id="m' + (i+1) + '" class="m on"><a href="'+url+'" class="aa1 mmm">'+first.label+'</a>';

            //二级菜单
            if (first.children && first.children.length > 0) {
                navHtml+= '<div class="sub"><div class="nav_xiao"><div class="xiao_five2">';
                var secondList = first.children;
                for (var j=0; j<secondList.length; j++) {
                    var second = secondList[j];
                    var marginLeft = "10px";
                    var urlSec = getLink(second);

                    //二级采单间距
                    var width = 300;
                    if (1 == second.type) {
                        width = 160;
                    }
                    if (second.children && second.children.length > 0) {
                        width = 300;
                    }

                    var floatDirect = "left";
                    if (2 == second.type) {
                        width = parseInt(second.imgWidth) + 20;
                        floatDirect = "left";
                    }

                    navHtml += '<div style="width:' + width + 'px; float:' + floatDirect + '; margin-' + floatDirect +
                        ':' + marginLeft + '">';

                    if (1 == second.type) {
                        navHtml += '<a style="width:' + width + 'px; text-align:left;" href="' + urlSec + '">' +
                            second.label + '</a>';
                    }

                    if (2 == second.type) {
                        navHtml += '<a href="' + urlSec + '"><div style="width: ' + second.imgWidth +
                            'px; text-align: center; float: left;"><img src="' + second.img + '" border="0" width="' +
                            second.imgWidth + '" height="' + second.imgHeight + '" /></div><div style="width: '+
                            second.imgWidth +'px; text-align: center; float: left">' + second.label + '</div></a>';
                    }

                    if (3 == second.type) {
                        navHtml += '<a style="width:' + width + 'px; text-align:left; line-height: ' +
                            second.imgHeight+ 'px;" href="' + urlSec + '"><img src="' + second.img + '" border="0" ' +
                            'width="' + second.imgWidth + '" height="' + second.imgHeight + '" align="middle" />' +
                            second.label + '</a>';
                    }


                    //第三级采单
                    if (second.children && second.children.length > 0) {
                        navHtml += '<ul style="border-top:1px solid #e3e3e3; padding: 10px">';
                        var threeList = second.children;
                        for (var n=0; n<threeList.length; n++) {
                            var three = threeList[n];
                            var widthThree = width - 30;
                            var urlThree = getLink(three);

                            navHtml += '<li style="width: ' + widthThree + 'px">';

                            if (1 == three.type) {
                                navHtml += '<a href="' + urlThree + '">' + three.label + '</a>';
                            }

                            if (2 == three.type) {
                                navHtml += '<a href="' + urlThree + '"><div style="width: ' + three.imgWidth +
                                    'px; text-align: center; float:left"><img src="' + three.img +
                                    '" border="0" width="'+ three.imgWidth +'" height="' + three.imgHeight +
                                    '" /> </div><div style="width: '+ three.imgWidth +'px; text-align: center; float: left">' +
                                    three.link + '</div></a>'
                            }

                            if (3 == three.type) {
                                navHtml += '<a class="suba" href="' + urlThree + '" style="line-height: ' +
                                    three.imgHeight + 'px"><img src="' + three.img + '" border="0" width="' +
                                    three.imgWidth + '" height="' + three.imgHeight + '" align="middle" />' +
                                    three.label + '</a>';
                            }

                            navHtml += '</li>'
                        }
                        navHtml += '</ul>';
                    }

                    navHtml += '</div>';
                }

                navHtml+= '</div></div></div>';
            }
            navHtml += '</li>';
        }

        $('#hl-default-nav').html(navHtml);

        jQuery(".nav").slide({ type:"menu",  titCell:".m", targetCell:".sub", effect:"slideDown", delayTime:300, triggerTime:100,returnDefault:true});
    });




    if(document.getElementById('webtitle')) document.getElementById('webtitle').innerHTML=webtitle;

    $("#hul li").click(function () {
        $.cookie("cur", "", { expires: -1 });
        var this_id = $(this).attr("id");
        $.cookie("cur", this_id,{path:"/"});
    });
    if ($.cookie("cur")!= null){
        var num1=$.cookie("cur");
        $("#"+num1).addClass('hover');
    }
    var service = {};
    var fn="logined";
    service = Commonjs.jsonToString(service)
    var params = Commonjs.getParams(fn,service);//获取参数
    $.ajax({
        datatype:"json",
        type:"POST",
        url: weburl,
        data:params,
        cache : false,
        success:function(data){
            var obj = jQuery.parseJSON(data);
            if (obj.result == "success") {
                $('#nologged').addClass('hide');
                $('#yeslogged').addClass('show');
                $.cookie(config.cookie.userName,obj.data.uname);
                if($.cookie(config.cookie.userName)!=null){
                    $('#hluserName').html("您好,"+obj.data.uname);
                }
            }else{
                $('#nologged').addClass('show');
                $('#yeslogged').addClass('hide');
                if(obj.result=='S202'){
                    $.cookie(config.cookie.userName, null,{expires:-1,path:"/"});
                }
            }
        },
        error:function(){
            $('#nologged').addClass('show');
            $('#yeslogged').addClass('hide');
        }
    });

    if ($.cookie(config.cookie.userName)==null || $.cookie(config.cookie.userName)=="undefined"){
        $("#cartCount").text(0);
    }else{
        queryUserCartCount();
    }
});

function queryUserCartCount(){
    var service = {};
    var fn="queryUserCartCount";
    service = Commonjs.jsonToString(service)
    var params = Commonjs.getParams(fn,service);//获取参数
    $.ajax({
        datatype:"json",
        type:"POST",
        url: weburl,
        data:params,
        cache : false,
        success: function(data){
            var data=jQuery.parseJSON(data);
            if(data.result == "success"){
                $("#cartCount").text("0");
                $("#cartCount").text(data.data.count);
            }
        }
    });
}

function unicplogin(){
    var service = {};
    var fn="unicplogin";
    service = Commonjs.jsonToString(service)
    var params = Commonjs.getParams(fn,service);//获取参数
    $.ajax({
        datatype:"json",
        type:"POST",
        url: weburl,
        data:params,
        cache : false,
        success: function(data){
            var data=jQuery.parseJSON(data);
            if(data.result=="success"){
                var url=data.msg;
                window.open(url);
            }else{
                Commonjs.alert(data.msg);
            }
        },
        error: function () {
            alertNew('服务器忙，请稍候再试！');
        }
    });
}

function icplogin(){
    var service = {};
    var fn="icplogin";
    service = Commonjs.jsonToString(service)
    var params = Commonjs.getParams(fn,service);//获取参数
    $.ajax({
        datatype:"json",
        type:"POST",
        url: weburl,
        data:params,
        cache : false,
        success: function(data){
            var data=jQuery.parseJSON(data);
            if(data.result=="success"){
                var url=data.msg;
                window.open(url);
            }else{
                Commonjs.alert(data.msg);
            }
        },
        error: function () {
            alertNew('服务器忙，请稍候再试！');
        }
    });
}

function changeManage() {
    var dialog=	art.dialog({
        lock : true,
        opacity : 0.4,
        width : 250,
        title : '提示',
        content : '确定要切换回管理后台？',
        ok : function() {
            var service = {};
            var fn="backAdmin";
            service = Commonjs.jsonToString(service);
            var params = Commonjs.getParams(fn,service);//获取参数
            Commonjs.ajaxTrue(weburl,params,function (data) {
                window.location.href=configParam.htmlPage.backGround;
            });
        },
        cancel: function(){
            $('#dialog').hide();
        }
    });
    $(".aui_close").hide();//隐藏关闭
}